import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

const baseUrl = CommonConfig.miotUrl + '/v1'
const baseUrlV3 = CommonConfig.miotUrl + '/v3'
const tpmUrl = CommonConfig.tpmUrl + '/v1'
const energyUrl = CommonConfig.energy + '/v1'
const machinestatusUrl = CommonConfig.machinestatusUrl + '/v1'
const bmsUrl = CommonConfig.bmsUrl + '/v1'

/**
 * ucloud获取plc产品列表
 */
export function getPlcProducts () {
  const newUrl = baseUrl + '/box/plc/products'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * ucloud创建子设备
 */
export function createPlcDevice (data) {
  const newUrl = baseUrl + '/box/plc/create'

  return axiosInstance.post(newUrl, null, { params: data }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 批量创建设备
 * @param data
 */
export function createPatchDevice (data) {
  const newUrl = baseUrl + '/box/plccnc/create'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 部署ucloud
 */
export function deployUcloud (data) {
  const newUrl = baseUrl + '/box/ucloud/deploy'

  return axiosInstance.post(newUrl, null, { params: data }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 部署ucloud
 */
export function deployUcloudV3 (data) {
  const newUrl = baseUrlV3 + '/box/ucloud/deploy'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 重启盒子
 */
export function boxRestartV3 (boxId) {
  const newUrl = baseUrlV3 + `/box/restart?boxId=${boxId}`

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据盒子id删除实时数据缓存
 * @param boxid
 */
export function removePlcCache (boxId) {
  const newUrl = machinestatusUrl + `/remove_plcdata_cache_boxid?boxId=${boxId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * ucloud获取plc网关可绑定驱动列表
 */
export function getBindDriverList (params) {
  const newUrl = baseUrl + '/box/plc/bindDriverList'

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 网关驱动绑定
 */
export function plcDriverBind (data) {
  const newUrl = baseUrl + '/box/plc/ucloudDriverBind'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 网关驱动绑定
 */
export function allDriverBind (data) {
  const newUrl = baseUrlV3 + '/box/ucloudDriverBind'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取物模型
 */
export function getBoxProducts (data) {
  const newUrl = baseUrl + '/box/products'

  return axiosInstance.get(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 网关驱动更新子设备配置
 */
export function plcPointConfigSet (data) {
  const newUrl = baseUrl + '/box/plc/ucloudDriverSubDevConf'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 写入点位
 */
export function plcDeviceBindPoint (data) {
  const newUrl = tpmUrl + '/device_bind_point'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 能耗点位
 */
export function energyDeviceBindPoint (data) {
  const newUrl = energyUrl + '/instruPlcPoint/add'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 修改plc的lan信息
 * @param gatewayId
 * @param productId
 * @param params
 */
export function updatePlcLan (params) {
  const newUrl = baseUrl + `/box/plc/lan/update`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * plcUcloud盒子基础信息查询
 * @param boxId
 */
export function getPlcBoxDriverInfo (boxId) {
  const newUrl = baseUrl + `/box/plc/box_info_query/${boxId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * plcUcloud盒子LAN网配置查询
 * @param boxId
 */
export function getPlcLanInfo (boxId) {
  const newUrl = baseUrl + `/box/plc/lan_query/${boxId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function getPlcDeviceByBoxId (boxId) {
  const newUrl = tpmUrl + `/plcDevice_By_BoxId`

  return axiosInstance.get(newUrl, { params: { boxId } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询设备绑定点位(列表)
 * @param boxId
 */
export function getPlcPoint (deviceIds) {
  const newUrl = tpmUrl + `/device/points`

  let config = {
    params: {
      deviceIds: deviceIds
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.get(newUrl, config).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * ucloud获取plc网关可绑定驱动列表
 * @param params
 */
export function getPlcDriverInfo (boxId, productId) {
  const newUrl = baseUrl + `/box/plc/box_driver_query/${boxId}`

  return axiosInstance.get(newUrl, { params: { hwBoxId: productId } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * plc删除盒子
 * @param boxId
 * @param modelType 0:plc 1:cnc
 */
export function deletePlcBox (boxId, modelType = 0, ucloudBoxId) {
  const newUrl = baseUrl + `/box/plc/delete?boxId=${boxId}&modelType=${modelType}&ucloudBoxId=${ucloudBoxId}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * plc删除盒子
 * @param boxId
 * @param ucloudBoxId
 */
export function deleteLatestBox (boxId, ucloudBoxId, updateVersion, boxTableId) {
  const newUrl = baseUrlV3 + `/box/delete?boxId=${boxId}&ucloudBoxId=${ucloudBoxId}&updateVersion=${updateVersion}&boxTableId=${boxTableId}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 下载点位模板
 */
export function downloadPointTemplate (name, productId) {
  const newUrl = `${baseUrl}/minio/point/download/${productId}?name=${name}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 下载能耗点位模板
 */
export function downloadEnergyPointTemplate (name, productId) {
  const newUrl = `${baseUrlV3}/minio/energy/point/download`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 导出plc点位数据
 * @param params
 * @returns {Promise<AxiosResponse<any>>}
 */
export function exportPlcPoint (params) {
  const newUrl = `${baseUrl}/minio/plcpoint/export`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 导出能耗点位数据
 * @param params
 * @returns {Promise<AxiosResponse<any>>}
 */
export function exportEnergyPoint (params) {
  const newUrl = `${baseUrlV3}/minio/energy/point/export`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 上传点位模板
 */
export function uploadPointTemplate (params) {
  const newUrl = `${tpmUrl}/minio/point/import`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 上传点位模板
 */
export function uploadEnergyPointTemplate (params) {
  const newUrl = `${baseUrlV3}/minio/energy/point/import`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * plcUcloud盒子应用信息查询
 * @param boxId
 */
export function getBoxApplicationQuery (boxId) {
  const newUrl = `${baseUrl}/box/plc/box_application_query/${boxId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * ucloud-升级网关应用版本
 * @param params
 * @returns {Promise<AxiosResponse<any>>}
 */
export function upgradeUcloudUpApplication (params) {
  const newUrl = `${baseUrl}/box/ucloudUpApplication`

  return axiosInstance.post(newUrl, null, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询设备未使用通用关联点位
 * @param boxId
 */
export function getCommonPoints (deviceIds) {
  const newUrl = tpmUrl + `/device/commonPoints`

  let config = {
    params: {
      deviceIds: deviceIds
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.get(newUrl, config).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 请求miot网关详情
 * @param ucloudBoxId
 */
export function getBoxDetail (ucloudBoxId) {
  const newUrl = `${baseUrl}/gateway_detail/send/${ucloudBoxId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子创建
 * boxId 盒子id
 * productId 产品id
 * productName 产品名称
 */
export function createLatestBox (gatewaySN, productSN, productName) {
  const newUrl = baseUrlV3 + `/box/create?gatewaySN=${gatewaySN}&productSN=${productSN}&productName=${productName}`

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function createPatchBox (data) {
  const newUrl = baseUrlV3 + `/box/creates`

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取盒子已经绑定工单驱动
export function getBoxDriverList (boxId, updateVersion = '2') {
  const newUrl = baseUrlV3 + `/box/plc/box_driver_list/${boxId}`

  return axiosInstance.get(newUrl, { params: { updateVersion } }).then((res) => {
    return Promise.resolve(res)
  })
}

// 删除驱动
export function deleteBoxDriver (data) {
  const newUrl = baseUrlV3 + `/box/driver/delete`

  return axiosInstance.delete(newUrl, { data }).then((res) => {
    return Promise.resolve(res)
  })
}

// 网关驱动配置查询
export function getBoxDriverParams (data) {
  const newUrl = baseUrlV3 + `/box/plc/box_driver_query`

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// ucloud-cnc网关驱动
export function driverSubDevConf (data) {
  const newUrl = baseUrlV3 + '/box/ucloudDriverSubDevConf'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 绑定点位
export function plcPointConfigSetV2 (data) {
  const newUrl = baseUrlV3 + '/box/plc/ucloudDriverSubDevConf'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询设备绑定点位(列表)
 * @param boxId
 */
export function getBindPoint (boxDriverId) {
  const newUrl = baseUrlV3 + `/box/box_driver_pointlist/${boxDriverId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取能源类型list
 */
export function getEnergyTypeList () {
  const newUrl = energyUrl + `/energy_type_list`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取能耗设备
 * @param energyTypeId 能耗类型
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getEnergyDevices (energyTypeId) {
  const newUrl = energyUrl + `/instru_list_by_energy?energyTypeId=${energyTypeId}&autoFlag=0`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取plc已绑定的设备
 * @param energyTypeId
 */
export function getPlcBindDevice (energyTypeId) {
  const newUrl = tpmUrl + `/device_num_with_box_tenant`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取能耗已绑定的设备
 * @param energyTypeId
 */
export function getEnergyBindDevice () {
  const newUrl = energyUrl + `/instru/listBytenantWithBox`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function energyUseBoxNum () {
  const newUrl = energyUrl + `/instru/bind_gateway_list`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据盒子id删除实时数据缓存
 * @param boxid
 */
export function getUserByDeviceId (deviceId) {
  const newUrl = machinestatusUrl + `/user/device_id?deviceId=${deviceId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function downloadMiotData (params) {
  const newUrl = baseUrl + `/box/like_query_list/export`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 *
 * @param params
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getAppInfo () {
  const newUrl = bmsUrl + `/appCompany/listByTenant`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子绑定应用
 */
export function boxBindApp (data) {
  const newUrl = baseUrlV3 + '/box/ucloudAppBind'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * ucloud网关应用编辑
 * @returns {Promise<AxiosResponse<any>>}
 */
export function boxEditApp (data) {
  const newUrl = baseUrlV3 + '/box/ucloudAppEdit'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * ucloud网关应用配置
 * @returns {Promise<AxiosResponse<any>>}
 */
export function editAppConfig (data) {
  const newUrl = baseUrlV3 + '/box/ucloudAppConfig'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * ucloud网关应用删除
 * @param data
 */
export function boxAppRemove (data) {
  const newUrl = baseUrlV3 + '/box/ucloudAppRemove'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据应用获取附件url
 * @param appId
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getAppFileUrl (appId) {
  const newUrl = bmsUrl + `/appCompany/fileget/${appId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 网络诊断
 * @param data
 */
export function boxNetFix (data) {
  const newUrl = baseUrlV3 + '/box/netFix'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}
